﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace GenBenEditor
{
    public partial class MiniMap : UserControl
    {
        private const int minipage_width = 16;
        private const int minipage_height = 9;
        private const int minipages_x = 8;
        private const int minipages_y = 14;

        public delegate void RepaintMapHandler();
        public event RepaintMapHandler repaint_map;

        public Map map;
        public int selected_page_x = 127;
        public int selected_page_y = 127;

        public MiniMap()
        {
            InitializeComponent();
            minimapBox.Paint += new PaintEventHandler(minimapBox_Paint);
            minimapBox.MouseClick += new MouseEventHandler(minimapBox_MouseClick);
        }

        private void minimapBox_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                int clicked_page_x = selected_page_x - (minipages_x / 2) + ((e.X - 1) / minipage_width);
                int clicked_page_y = selected_page_y - (minipages_y / 2) + ((e.Y - 1) / minipage_height);

                if (clicked_page_x < 0 || clicked_page_x > 255 || clicked_page_y < 0 || clicked_page_y > 255)
                    return;

                selected_page_x = clicked_page_x;
                selected_page_y = clicked_page_y;

                coordLabel.Text = String.Format("X = {0}; Y = {1}", selected_page_x, selected_page_y);
                Invalidate();
                repaint_map();
            }
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            minimapBox.Invalidate();
            base.OnPaint(e);
        }

        private void minimapBox_Paint(object sender, PaintEventArgs e)
        {
            PictureBox pbox = (PictureBox)sender;
            Rectangle bounds = pbox.ClientRectangle;
            bounds.Width -= 1;
            bounds.Height -= 1;
            e.Graphics.FillRectangle(Brushes.White, pbox.ClientRectangle);
            e.Graphics.DrawRectangle(Pens.Black, bounds);

            if (map == null)
                return;

            for (int y = 0; y < minipages_y; ++y)
            {
                for (int x = 0; x < minipages_x; ++x)
                {
                    int page_x = selected_page_x - (minipages_x / 2) + x;
                    int page_y = selected_page_y - (minipages_y / 2) + y;

                    if (page_x < 0 || page_x > 255 || page_y < 0 || page_y > 255)
                        continue;

                    Brush color;
                    if (page_y == selected_page_y && page_x == selected_page_x)
                        color = Brushes.DarkRed;
                    else if (map.positioned_pages[page_y, page_x] != null)
                        color = Brushes.DarkBlue;
                    else
                        color = Brushes.LightSlateGray;

                    e.Graphics.FillRectangle(color, 1 + x * minipage_width,
                           1 + y * minipage_height, minipage_width, minipage_height);
                    e.Graphics.DrawRectangle(Pens.Black, 1 + x * minipage_width,
                        1 + y * minipage_height, minipage_width - 1, minipage_height - 1);
                }
            }
        }
    }
}
